#include "rank.h"
#include "ui_rank.h"
#include "database/usersdb.h"

Rank::Rank(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Rank)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window);
    //定义数据模型
    model = new QSqlQueryModel(this);
    //给数据模型设置查询sql语句
    model->setQuery("select name, score from simpleRank "
                    "order by score desc limit 10");
    model->setHeaderData(0, Qt::Horizontal, tr("姓名"));
    model->setHeaderData(1, Qt::Horizontal, tr("分数"));

    // 设置编辑策略
    ui->tableView->setModel(model);
    //设置表格显示长度
    ui->tableView->setColumnWidth(0, 220);
    ui->tableView->setColumnWidth(1, 215);

    //设置背景图片
    QPalette palette = this->palette();
    palette.setBrush(this->backgroundRole(),QBrush(QPixmap(":/resources/write.jpg")));
    this->setPalette(palette);
}

Rank::~Rank()
{
    delete ui;
}

/**
 * @brief 排行榜
 *
 */
void Rank::rankShow()
{
    UsersDB *userdb = new UsersDB;
    UserModel user = userdb->getUserInfo();

    int fontsize = userdb->getFontSize(user.name);
    //设置事例字体点阵
    QFont font;
    font.setPointSize(fontsize);
    this->ui->tableView->setFont(font);

    this->ui->comboBox->setCurrentIndex(user.difficulty);
    this->on_comboBox_currentIndexChanged(user.difficulty);
    this->show();
}

/**
 * @brief 定位不同难度
 *
 * @param index comboBox改变到的节点
 */
void Rank::on_comboBox_currentIndexChanged(int index)
{
    QString table = "simpleRank";
    switch (index) {
    case 0:
        table = "simpleRank";
        break;
    case 1:
        table = "generalRank";
        break;
    case 2:
        table = "difficultRank";
        break;
    default:
        break;
    }
    model->setQuery(QString("select name as 姓名, score as 分数 from %1 "
                            "order by score desc limit 10").arg(table));
}

/**
 * @brief 返回
 *
 */
void Rank::on_pushButton_clicked()
{
    switch (StatusValue::status) {
    case Load:
        break;
    case Ready:
        this->hide();
        emit enterBegin();
        break;
    case Playing:
        break;
    case Pausing:
        this->hide();
        emit enterPause();
    default:
        break;
    }
}
